package com.seres.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.formula.functions.Match;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class DataManager {
	
	public static void main(String[] args){
		DataManager dm = new DataManager();
		
		//Answer (create - update - delete)
//		int impactId1 = dm.createStakeholderImpact(131, Stakeholder.AUTHORITIES);
//		int impactId2 = dm.createStakeholderImpact(141, Stakeholder.CLINICIANS);
//		int impactId3 = dm.createIndexImpact(151, Index.RISK);
//		int impactId4 = dm.createIndexImpact(161, Index.SHORT_TERM_IMPACT);
//		
//		List<Integer> shimpacts = new ArrayList<Integer>();
//		shimpacts.add(impactId1);
//		shimpacts.add(impactId2);
//		List<Integer> indeximpacts = new ArrayList<Integer>();
//		indeximpacts.add(impactId3);
//		indeximpacts.add(impactId4);
//		int answerId = dm.createAnswer("testanswer3", "dit is een testanswer3", indeximpacts, shimpacts, false, false, 0);
//		
//		List<Integer> possibleAnswers = new ArrayList<Integer>();
//		possibleAnswers.add(answerId);
//		
//		int scenarioId = dm.createScenario("scenario x", "this is scenario x", possibleAnswers);
		
		// create CoreModule
//		List<Stakeholder> shToEval = new ArrayList<Stakeholder>();
//		shToEval.add(Stakeholder.AUTHORITIES);
//		
//		int moduleId = dm.createCoreModule(false, false, shToEval, 1);
//		
		//read CoreModule
//		Module mod = dm.readModule(3);
//		if(mod.getModuleType() == ModuleType.CORE){
//			CoreModule coreMod = (CoreModule) mod;
//			List<Stakeholder> stakeholders = coreMod.getStakeholdersToEvaluate();
//			Iterator it = stakeholders.iterator();
//			while(it.hasNext()){
//				System.out.println(it.next().toString());
//			}
//		}
		
		// create GameStructure
//		List<Integer> moduleIds = new ArrayList<Integer>();
//		moduleIds.add(moduleId);
//		int gameStructId = dm.createGameStructure("struct x", null, null, moduleIds);
//		
//		GameStructure gs = dm.readGameStructure(gameStructId);
//		System.out.println("game structure name: " + gs.getStructureName());
//		System.out.println("nr of core modules: " + gs.getCoreModules().size());
		
		
//		Answer answer = dm.readAnswer(4);
//		System.out.println(answer);
//		System.out.println(answer.getAnswerId());
//		System.out.println(answer.getFullDescription());
//		System.out.println(answer.getIndexImpacts().get(0).getValue());
//		
//		List<StakeholderImpact> impacts = answer.getStakeholderImpacts();
//		Iterator it = impacts.iterator();
//		while(it.hasNext()){
//			StakeholderImpact stakeholderImpact = (StakeholderImpact) it.next();
//			System.out.println("sh impact value: " + stakeholderImpact.getValue());
//		}
//		
//		answer.setFullDescription("dit is het antwoord na update5");
//		dm.updateAnswer(answer);
//		
//		Answer answer2 = dm.readAnswer(1);
//		System.out.println(answer2);
//		System.out.println(answer2.getAnswerId());
//		System.out.println(answer2.getFullDescription());
		
//		dm.deleteAnswer(1);
		
		//Acronym (create - update - delete)
//		dm.createAcronym("A", "test description");
//		Acronym acr = dm.readAcronym("A");
//		
//		acr.setDescription("laatste check");
//		
//		dm.updateAcronym(acr);
//		dm.deleteAcronym("A");
		
		//AceCategory & Ace (create - update)
//		dm.createAceCategory("cat 5", null);
//		int aceId = dm.createAce("dit is ace 5", "ace 5", null, null);
//		Ace ace = dm.readAce(aceId);
//		AceCategory aceCat = dm.readAceCategory("cat 5");
//		
//		System.out.println(aceCat.getAces().size());
//		aceCat.addAce(ace);
		
//		dm.updateAceCategory(aceCat);
		
		//create Message & RiskModule
//		int impactId1 = dm.createStakeholderImpact(5, Stakeholder.EMPLOYEES);
//		int impactId2 = dm.createStakeholderImpact(1, Stakeholder.SMCIES);
//		int impactId3 = dm.createStakeholderImpact(1, Stakeholder.INTERNAL_PEERS);
//		int impactId4 = dm.createStakeholderImpact(1, Stakeholder.PROF_SOCIETIES);
//		int impactId5 = dm.createStakeholderImpact(1, Stakeholder.KOLS);
//		int impactId6 = dm.createStakeholderImpact(1, Stakeholder.CLINICIANS);
//		int impactId7 = dm.createStakeholderImpact(1, Stakeholder.PRESS);
//		int impactId8 = dm.createStakeholderImpact(1, Stakeholder.AUTHORITIES);
//		int impactId9 = dm.createIndexImpact(1, Index.RISK);
//		
//		List<Integer> shimpacts = new ArrayList<Integer>();
//		shimpacts.add(impactId1);
//		shimpacts.add(impactId2);
//		shimpacts.add(impactId3);
//		shimpacts.add(impactId4);
//		shimpacts.add(impactId5);
//		shimpacts.add(impactId6);
//		shimpacts.add(impactId7);
//		shimpacts.add(impactId8);
//		List<Integer> indeximpacts = new ArrayList<Integer>();
//		indeximpacts.add(impactId9);
//		
//		int description1 = dm.createDescription("test description 1");
//		int description2 = dm.createDescription("test description 2");
//		List<Integer> descriptions = new ArrayList<Integer>();
//		descriptions.add(description1);
//		descriptions.add(description2);
//		
//		int answer1 = dm.createAnswer("short answer 1", "full answer 1", indeximpacts, shimpacts, false, false, 0);
//		
//		impactId1 = dm.createStakeholderImpact(5, Stakeholder.EMPLOYEES);
//		impactId2 = dm.createStakeholderImpact(1, Stakeholder.SMCIES);
//		impactId3 = dm.createStakeholderImpact(1, Stakeholder.INTERNAL_PEERS);
//		impactId4 = dm.createStakeholderImpact(1, Stakeholder.PROF_SOCIETIES);
//		impactId5 = dm.createStakeholderImpact(1, Stakeholder.KOLS);
//		impactId6 = dm.createStakeholderImpact(1, Stakeholder.CLINICIANS);
//		impactId7 = dm.createStakeholderImpact(1, Stakeholder.PRESS);
//		impactId8 = dm.createStakeholderImpact(1, Stakeholder.AUTHORITIES);
//		impactId9 = dm.createIndexImpact(1, Index.RISK);
//		
//		shimpacts = new ArrayList<Integer>();
//		shimpacts.add(impactId1);
//		shimpacts.add(impactId2);
//		shimpacts.add(impactId3);
//		shimpacts.add(impactId4);
//		shimpacts.add(impactId5);
//		shimpacts.add(impactId6);
//		shimpacts.add(impactId7);
//		shimpacts.add(impactId8);
//		indeximpacts = new ArrayList<Integer>();
//		indeximpacts.add(impactId9);
//		
//		int answer2 = dm.createAnswer("short answer 2", "full answer 2", indeximpacts, shimpacts, false, false, 0); 
//		
//		impactId1 = dm.createStakeholderImpact(5, Stakeholder.EMPLOYEES);
//		impactId2 = dm.createStakeholderImpact(1, Stakeholder.SMCIES);
//		impactId3 = dm.createStakeholderImpact(1, Stakeholder.INTERNAL_PEERS);
//		impactId4 = dm.createStakeholderImpact(1, Stakeholder.PROF_SOCIETIES);
//		impactId5 = dm.createStakeholderImpact(1, Stakeholder.KOLS);
//		impactId6 = dm.createStakeholderImpact(1, Stakeholder.CLINICIANS);
//		impactId7 = dm.createStakeholderImpact(1, Stakeholder.PRESS);
//		impactId8 = dm.createStakeholderImpact(1, Stakeholder.AUTHORITIES);
//		impactId9 = dm.createIndexImpact(1, Index.RISK);
//		
//		shimpacts = new ArrayList<Integer>();
//		shimpacts.add(impactId1);
//		shimpacts.add(impactId2);
//		shimpacts.add(impactId3);
//		shimpacts.add(impactId4);
//		shimpacts.add(impactId5);
//		shimpacts.add(impactId6);
//		shimpacts.add(impactId7);
//		shimpacts.add(impactId8);
//		indeximpacts = new ArrayList<Integer>();
//		indeximpacts.add(impactId9);
//		
//		int answer3 = dm.createAnswer("short answer 3", "full answer 3", indeximpacts, shimpacts, false, false, 0);
//		List<Integer> possibleAnswers = new ArrayList<Integer>();
//		possibleAnswers.add(answer1);
//		possibleAnswers.add(answer2);
//		possibleAnswers.add(answer3);
//		
//		int im1 = dm.createSeresImage("1.jpg");
//		int im2 = dm.createSeresImage("2.jpg");
//		List<Integer> images = new ArrayList<Integer>();
//		images.add(im1);
//		images.add(im2);
//		
//		int scenarioId = dm.createScenario("short scenario description", descriptions, possibleAnswers, images, "test scenario question");
//		
//		int moduleId = dm.createSHModule(scenarioId, Stakeholder.EMPLOYEES, "test sh module");
//		Message message = dm.readMessage(messageId);
//		System.out.println(message.getMessage());
//		Module mod2 = dm.readModule(moduleId);
//		if(mod2.getModuleType() == ModuleType.RISK){
//			RiskModule riskMod = (RiskModule) mod2;
//			System.out.println("risk module id: " + riskMod.getModuleId());
//		}
		
	}
	
	// CREATE
	public int createIndexImpact(float value, Index index){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int impactId = 0;
		
		try {
			transaction = session.beginTransaction();
		
			IndexImpact indexImpact = new IndexImpact(value,index);
			impactId = (Integer) session.save(indexImpact);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return impactId;
	}
	public int createStakeholderImpact(float value, Stakeholder stakeholder){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int impactId = 0;
		
		try {
			transaction = session.beginTransaction();
		
			StakeholderImpact stakeholderImpact = new StakeholderImpact(value,stakeholder);
			impactId = (Integer) session.save(stakeholderImpact);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return impactId;
	}
	public int createAnswer(String shortDescription, String fullDescription, List<Integer> indexImpacts, List<Integer> stakeholderImpacts, 
			boolean risky, boolean corrective, int nextScenarioId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int answerId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			List<IndexImpact> indexImpactsObjects = null;
			if(indexImpacts != null){
				indexImpactsObjects = new ArrayList<IndexImpact>();
				Iterator<Integer> it1 = indexImpacts.iterator();
				while(it1.hasNext()){
					IndexImpact indexImpact = (IndexImpact) session.get(IndexImpact.class, (Integer) it1.next());
					indexImpactsObjects.add(indexImpact);
				}
			}

			List<StakeholderImpact> stakeholderImpactsObjects = null;
			if(stakeholderImpacts != null){
				stakeholderImpactsObjects = new ArrayList<StakeholderImpact>();
				Iterator<Integer> it2 = stakeholderImpacts.iterator();
				while(it2.hasNext()){
					StakeholderImpact stakeholderImpact = (StakeholderImpact) session.get(StakeholderImpact.class, (Integer) it2.next());
					stakeholderImpactsObjects.add(stakeholderImpact);
				}
			}
			
			Scenario scenario = null;
			if(nextScenarioId != 0){
				scenario = (Scenario) session.get(Scenario.class, (Integer) nextScenarioId);
			}
			
			Answer answer = new Answer(fullDescription,shortDescription,indexImpactsObjects,stakeholderImpactsObjects,risky,corrective,scenario);
			answerId = (Integer) session.save(answer);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return answerId;
	}
//	public int createScenario(String shortDescription, String fullDescription, List<Integer> possibleAnswers){
	public int createScenario(String shortDescription, List<Integer> descriptions, List<Integer> possibleAnswers, List<Integer> images, String question){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int scenarioId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			List<Answer> answerObjects = null;
			if(possibleAnswers != null){
				answerObjects = new ArrayList<Answer>();
				Iterator<Integer> it1 = possibleAnswers.iterator();
				while(it1.hasNext()){
					Answer answer = (Answer) session.get(Answer.class, (Integer) it1.next());
					answerObjects.add(answer);
				}
			}
			List<Description> descriptionObjects = null;
			if(descriptions != null){
				descriptionObjects = new ArrayList<Description>();
				Iterator<Integer> it1 = descriptions.iterator();
				while(it1.hasNext()){
					Description description = (Description) session.get(Description.class, (Integer) it1.next());
					descriptionObjects.add(description);
				}
			}
			List<SeresImage> seresImageObjects = null;
			if(images != null){
				seresImageObjects = new ArrayList<SeresImage>();
				Iterator<Integer> it1 = images.iterator();
				while(it1.hasNext()){
					SeresImage seresImage = (SeresImage) session.get(SeresImage.class, (Integer) it1.next());
					seresImageObjects.add(seresImage);
				}
			}
			
//			Scenario scenario = new Scenario(shortDescription,fullDescription,answerObjects);
//			Scenario scenario = new Scenario(shortDescription,descriptionObjects,answerObjects,seresImageObjects);
			Scenario scenario = new Scenario(shortDescription,descriptionObjects,answerObjects,seresImageObjects,question);
			
			scenarioId = (Integer) session.save(scenario);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return scenarioId;
	}
	public int createDescription(String description){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int descriptionId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Description descriptionObject = new Description(description);
			descriptionId = (Integer) session.save(descriptionObject);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return descriptionId;
	
	}
	public int createSeresImage(String imageName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int seresImageId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			SeresImage seresImage = new SeresImage(imageName);
			seresImageId = (Integer) session.save(seresImage);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return seresImageId;
	
	}
	public int createCoreModule(boolean evaluationOfRiskIndexIndicator, boolean evaluationOfAllSHIndicator, List<Stakeholder> stakeholdersToEvaluate, int firstScenarioId, String title){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int moduleId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Scenario scenario = null;
			if(firstScenarioId != 0){
				scenario = (Scenario) session.get(Scenario.class, (Integer) firstScenarioId);
			}
			
			CoreModule coreModule = new CoreModule(evaluationOfRiskIndexIndicator,evaluationOfAllSHIndicator,stakeholdersToEvaluate,scenario,title);
			moduleId = (Integer) session.save(coreModule);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return moduleId;
	}
	public int createSHModule(int firstScenarioId, Stakeholder stakeholder, String title){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int moduleId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Scenario scenario = null;
			if(firstScenarioId != 0){
				scenario = (Scenario) session.get(Scenario.class, (Integer) firstScenarioId);
			}
			
			SHModule shModule = new SHModule(scenario,stakeholder,title);
			moduleId = (Integer) session.save(shModule);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return moduleId;
	}
	public int createRiskModule(boolean evaluationOfAllSHIndicator, int messageId, String title){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int moduleId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Message message = null;
			if(messageId != 0){
				message = (Message) session.get(Message.class, (Integer) messageId);
			}
			
			RiskModule riskModule = new RiskModule(evaluationOfAllSHIndicator, message, title);
			moduleId = (Integer) session.save(riskModule);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return moduleId;
	}
	public int createMessage(String message, List<Integer> indexImpacts, List<Integer> stakeholderImpacts){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int messageId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			List<IndexImpact> indexImpactsObjects = null;
			if(indexImpacts != null){
				indexImpactsObjects = new ArrayList<IndexImpact>();
				Iterator<Integer> it1 = indexImpacts.iterator();
				while(it1.hasNext()){
					IndexImpact indexImpact = (IndexImpact) session.get(IndexImpact.class, (Integer) it1.next());
					indexImpactsObjects.add(indexImpact);
				}
			}

			List<StakeholderImpact> stakeholderImpactsObjects = null;
			if(stakeholderImpacts != null){
				stakeholderImpactsObjects = new ArrayList<StakeholderImpact>();
				Iterator<Integer> it2 = stakeholderImpacts.iterator();
				while(it2.hasNext()){
					StakeholderImpact stakeholderImpact = (StakeholderImpact) session.get(StakeholderImpact.class, (Integer) it2.next());
					stakeholderImpactsObjects.add(stakeholderImpact);
				}
			}
			
			Message messageObject = new Message(message,indexImpactsObjects,stakeholderImpactsObjects);
			messageId = (Integer) session.save(messageObject);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return messageId;
	}
	public int createAce(String fullDescription, String shortDescription, List<Integer> indexImpacts, List<Integer> stakeholderImpacts){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int aceId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			List<IndexImpact> indexImpactsObjects = null;
			if(indexImpacts != null){
				indexImpactsObjects = new ArrayList<IndexImpact>();
				Iterator<Integer> it1 = indexImpacts.iterator();
				while(it1.hasNext()){
					IndexImpact indexImpact = (IndexImpact) session.get(IndexImpact.class, (Integer) it1.next());
					indexImpactsObjects.add(indexImpact);
				}
			}

			List<StakeholderImpact> stakeholderImpactsObjects = null;
			if(stakeholderImpacts != null){
				stakeholderImpactsObjects = new ArrayList<StakeholderImpact>();
				Iterator<Integer> it2 = stakeholderImpacts.iterator();
				while(it2.hasNext()){
					StakeholderImpact stakeholderImpact = (StakeholderImpact) session.get(StakeholderImpact.class, (Integer) it2.next());
					stakeholderImpactsObjects.add(stakeholderImpact);
				}
			}
			
			Ace ace = new Ace(fullDescription,shortDescription,indexImpactsObjects,stakeholderImpactsObjects);
			aceId = (Integer) session.save(ace);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return aceId;
	}
	public void createAceCategory(int versionId, String categoryName, List<Integer> aces){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			List<Ace> acesObjects = null;
			if(aces != null){
				acesObjects = new ArrayList<Ace>();
				Iterator<Integer> it1 = aces.iterator();
				while(it1.hasNext()){
					Ace ace = (Ace) session.get(Ace.class, (Integer) it1.next());
					acesObjects.add(ace);
				}
			}

			AceCategory aceCategory = new AceCategory(versionId,categoryName,acesObjects);
			session.save(aceCategory);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void createAcronym(String abbreviation, String description){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Acronym acronym = new Acronym(abbreviation, description);
			session.save(acronym);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public int createGameStructure(String structureName, List<Integer> riskModules, List<Integer> shModules, List<Integer> coreModules){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int gameStructureId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			List<RiskModule> riskModulesObjects = null;
			if(riskModules != null){
				riskModulesObjects = new ArrayList<RiskModule>();
				Iterator<Integer> it1 = riskModules.iterator();
				while(it1.hasNext()){
					RiskModule riskModule = (RiskModule) session.get(RiskModule.class, (Integer) it1.next());
					riskModulesObjects.add(riskModule);
				}
			}

			List<SHModule> shModulesObjects = null;
			if(shModules != null){
				shModulesObjects = new ArrayList<SHModule>();
				Iterator<Integer> it1 = shModules.iterator();
				while(it1.hasNext()){
					SHModule shModule = (SHModule) session.get(SHModule.class, (Integer) it1.next());
					shModulesObjects.add(shModule);
				}
			}
			
			List<CoreModule> coreModulesObjects = null;
			if(coreModules != null){
				coreModulesObjects = new ArrayList<CoreModule>();
				Iterator<Integer> it1 = coreModules.iterator();
				while(it1.hasNext()){
					CoreModule coreModule = (CoreModule) session.get(CoreModule.class, (Integer) it1.next());
					coreModulesObjects.add(coreModule);
				}
			}
			GameStructure gameStructure = new GameStructure(structureName,riskModulesObjects,shModulesObjects,coreModulesObjects);
			gameStructureId = (Integer) session.save(gameStructure);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return gameStructureId;
	}
	public int createStakeholder(Stakeholder stakeholder){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int stakeholderId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Stakeholder s = new Stakeholder(stakeholder);
			stakeholderId = (Integer) session.save(s);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return stakeholderId;
	}
	public int createIndex(Index index){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int indexId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			Index i = new Index(index);
			indexId = (Integer) session.save(i);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexId;
	}
	//////////////////
	// DELETE
	public void deleteAce(int aceId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Ace ace = (Ace) session.get(Ace.class, aceId);
			session.delete(ace);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteAceCategory(String categoryName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			AceCategory aceCategory = (AceCategory) session.get(AceCategory.class, categoryName);
			session.delete(aceCategory);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteAcronym(String abbreviation){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Acronym acronym = (Acronym) session.get(Acronym.class, abbreviation);
			session.delete(acronym);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteDescription(int descriptionId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Description description = (Description) session.get(Description.class, descriptionId);
			session.delete(description);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteSeresImage(int seresImageId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			SeresImage seresImage = (SeresImage) session.get(SeresImage.class, seresImageId);
			session.delete(seresImage);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteAnswer(int answerId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Answer answer = (Answer) session.get(Answer.class, answerId);
			session.delete(answer);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteCoreModule(int moduleId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			CoreModule module = (CoreModule) session.get(CoreModule.class, moduleId);
			session.delete(module);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteSHModule(int moduleId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			SHModule module = (SHModule) session.get(SHModule.class, moduleId);
			session.delete(module);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteRiskModule(int moduleId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			RiskModule module = (RiskModule) session.get(RiskModule.class, moduleId);
			session.delete(module);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteGameStructure(int gameStructureId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			GameStructure gameStructure = (GameStructure) session.get(GameStructure.class, gameStructureId);
			session.delete(gameStructure);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteIndexImpact(int impactId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			IndexImpact indexImpact = (IndexImpact) session.get(IndexImpact.class, impactId);
			session.delete(indexImpact);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteStakeholderImpact(int impactId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			StakeholderImpact stakeholderImpact = (StakeholderImpact) session.get(StakeholderImpact.class, impactId);
			session.delete(stakeholderImpact);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteMessage(int messageId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Message message = (Message) session.get(Message.class, messageId);
			session.delete(message);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void deleteScenario(int scenarioId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			Scenario scenario = (Scenario) session.get(Scenario.class, scenarioId);
			session.delete(scenario);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	//////////////////
	// UPDATE
	public void updateAce(Ace ace){
		updateObject(ace);
	}
	public void updateAceCategory(AceCategory aceCategory){
		updateObject(aceCategory);
	}
	public void updateAcronym(Acronym acronym){
		updateObject(acronym);
	}
	public void updateAnswer(Answer answer){
		updateObject(answer);
	}
	public void updateCoreModule(CoreModule coreModule){
		updateObject(coreModule);
	}
	public void updateSHModule(SHModule shModule){
		updateObject(shModule);
	}
	public void updateRiskModule(RiskModule riskModule){
		updateObject(riskModule);
	}
	public void updateGameStructure(GameStructure gameStructure){
		updateObject(gameStructure);
	}
	public void updateIndexImpact(IndexImpact indexImpact){
		updateObject(indexImpact);
	}
	public void updateStakeholderImpact(StakeholderImpact stakeholderImpact){
		updateObject(stakeholderImpact);
	}
	public void updateMessage(Message message){
		updateObject(message);
	}
	public void updateScenario(Scenario scenario){
		updateObject(scenario);
	}
	public void updateDescription(Description description){
		updateObject(description);
	}
	public void updateSeresImage(SeresImage seresImage){
		updateObject(seresImage);
	}
	private void updateObject(Object object){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			session.update(object);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	////////////////////////
	// READ
	public List<AceCategory> readAceCategories(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<AceCategory> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<AceCategory>) session.createQuery("from AceCategory")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public AceCategory readAceCategory(String categoryName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		AceCategory result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (AceCategory) session.get(AceCategory.class, categoryName);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Ace> readAces(String categoryName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Ace> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Ace>) session.createQuery("from Ace as ace where ace.categoryname = ?")
					.setString(0, categoryName)
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Ace> readAces(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Ace> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Ace>) session.createQuery("from Ace")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Ace readAce(int aceId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Ace result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Ace) session.get(Ace.class, aceId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Acronym> readAcronyms(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Acronym> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Acronym>) session.createQuery("from Acronym")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Acronym readAcronym(String abbreviation){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Acronym result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Acronym) session.get(Acronym.class, abbreviation);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Description readDescription(int descriptionId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Description result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Description) session.get(Description.class, descriptionId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public SeresImage readSeresImage(int seresImageId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		SeresImage result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (SeresImage) session.get(SeresImage.class, seresImageId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Answer> readUnassignedAnswers(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Answer> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Answer>) session.createQuery("from Answer as answer where answer.scenarioid = null")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Answer> readAnswers(int scenarioId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Answer> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Answer>) session.createQuery("from Answer as answer where answer.scenarioid = ?")
					.setInteger(0, scenarioId)
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Answer readAnswer(int answerId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Answer result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Answer) session.get(Answer.class, answerId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Module> readModules(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Module> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Module>) session.createQuery("from Module")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Module readModule(int moduleId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Module result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Module) session.get(Module.class, moduleId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<GameStructure> readGameStructures(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<GameStructure> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<GameStructure>) session.createQuery("from GameStructure")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public GameStructure readGameStructure(int gameStructureId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		GameStructure result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (GameStructure) session.get(GameStructure.class, gameStructureId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Message> readMessages(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Message> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Message>) session.createQuery("from Message")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Message readMessage(int messageId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Message result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Message) session.get(Message.class, messageId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Scenario> readScenarios(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Scenario> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Scenario>) session.createQuery("from Scenario")
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Scenario readScenario(int scenarioId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Scenario result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Scenario) session.get(Scenario.class, scenarioId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Impact readImpact(int impactId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Impact result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Impact) session.get(Impact.class, impactId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Stakeholder readStakeholder(int stakeholderId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Stakeholder result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Stakeholder) session.get(Stakeholder.class, stakeholderId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Index readIndex(int indexId){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Index result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (Index) session.get(Index.class, indexId);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public Index readIndex(String name){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Index result = null;
		try {
			transaction = session.beginTransaction();
			
			Criteria crit = session.createCriteria(Index.class);
			crit.add(Restrictions.ilike("name", name ,MatchMode.ANYWHERE)).addOrder( Order.desc("versionId"));
			
			@SuppressWarnings("rawtypes")
			List results = crit.list();
			if(results.get(0) != null){
				result = (Index) results.get(0);
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public int readLastStakeholderVersionId(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int maxVersionId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			@SuppressWarnings("rawtypes")
			List list = session.createQuery("select max(versionId) from Stakeholder").list();
			if(list.get(0) != null){
				maxVersionId = ((Integer) list.get(0)).intValue();				
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return maxVersionId;
	}
	public int readLastIndexVersionId(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int maxVersionId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			@SuppressWarnings("rawtypes")
			List list = session.createQuery("select max(versionId) from Index").list();
			if(list.get(0) != null){
				maxVersionId = ((Integer) list.get(0)).intValue();				
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return maxVersionId;
	}
	public int readLastAceCategoryVersionId(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int maxVersionId = 0;
		
		try {
			transaction = session.beginTransaction();
			
			@SuppressWarnings("rawtypes")
			List list = session.createQuery("select max(versionId) from AceCategory").list();
			if(list.get(0) != null){
				maxVersionId = ((Integer) list.get(0)).intValue();
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return maxVersionId;
	}
	public List<Stakeholder> readAllStakeholders(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Stakeholder> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Stakeholder>) session.createQuery("from Stakeholder as s where s.versionId = ?")
					.setInteger(0, readLastStakeholderVersionId())
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	public List<Index> readAllIndices(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Index> result = null;
		try {
			transaction = session.beginTransaction();
			
			result = (List<Index>) session.createQuery("from Index as i where i.versionId = ?")
					.setInteger(0, readLastIndexVersionId())
					.list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
}
